// pages/cardsercet/index.js
let app = getApp();
import Notify from '@vant/weapp/notify/notify';
let that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabbar_active: 2,
    introduction: '',
    image: '',
    show: false,
    code: '',
    userinfo: {}
  },
  loadUser(openid) {
    app.request('user/select/by/id', "POST", {
      id: openid
    }, 'xform').then(res => {
      that.setData({
        userinfo: res.data
      });
      wx.setStorageSync('userinfo', res.data);
      app.globalData.userinfo = res.data;
    })
  },
  showDialog() {
    let userinfo = this.data.userinfo;
    if (Object.keys(userinfo).length < 1) {
      Notify({
        type: 'danger',
        message: '请先登录',
        selector: '#notify_selector',
        onClose() {
          let currentPath = app.getCurrentPageUrl();
          wx.setStorageSync('currentPath', currentPath);
          wx.navigateTo({
            url: '/subPackage/view/user/login/index',
          })
        }
      })
    } else {
      if (this.data.userinfo.type != '永久用户') {
        this.setData({
          show: true
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.loadResource();
    that = this;
    this.setData({
      beforeClose: (action) => this.onBeforeClose(action)
    })
    let userinfo = wx.getStorageSync('userinfo');
    if (userinfo) {
      this.setData({
        userinfo: userinfo
      })
    }

  },
  loadResource() {
    let option = {
      pageNo: 1,
      pageSize: 1,
      type: ['eq', '会员介绍']
    };
    app.request('resource/select/by/page', 'POST', option).then(res => {
      let data = res.data.list;
      if (data.length > 0) {
        this.setData({
          introduction: data[0].content,
          image: data[0].coverImage
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  handlerTabbar(e) {
    this.setData({
      tabbar_active: e.detail
    });
  },
  onBeforeClose(action) {
    return new Promise((resolve) => {
      if (action == 'confirm') {
        let code = this.data.code;
        let openid = this.data.userinfo.id;
        if (!code) {
          Notify({
            type: 'danger',
            message: '请输入卡密',
            selector: '#notify_selector'
          })
          resolve(false);
          return;
        }
        app.request('card/activate/code', "POST", {
          code: code,
          openid: openid
        }, "xform").then(res => {
          Notify({
            type: 'success',
            message: '激活成功',
            selector: '#notify_selector'
          })
          that.loadUser(openid);
          resolve(true);
          that.setData({
            show: false
          })
        }).catch(e => {
          Notify({
            type: 'danger',
            message: e.data,
            selector: '#notify_selector'
          })
          resolve(false)
        })
      } else {
        that.setData({
          show: false
        })
        resolve(true)
      }
    })
  }
})
